package com.example.service;

import com.example.control.indexController;
import com.example.dao.articleMapper;
import com.example.dao.commentMapper;
import com.example.dao.statisticMapper;
import com.example.domain.t_article;
import com.example.domain.t_comment;
import com.example.domain.t_statistic;
import com.example.model.responseData.StaticticsBo;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//博客站点统计服务
@Service
@Transactional
public class ISiteService {
    private static final Logger logger = LoggerFactory.getLogger(indexController.class);

    @Autowired
    private commentMapper comm;

    @Autowired
    private articleMapper arti;

    @Autowired
    private statisticMapper stat;

    @Autowired
    private IUserService iuse;

    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate redis;
    //最近收到的评论

    public List<t_comment> recentComents(int count,int user_id){

        String aut = iuse.SelectAuthority(user_id);

        if(aut.contains("root")) {
            PageHelper.startPage(1, count <= 10 && count >= 1 ? count : 10);
            return comm.selectSortNews();
        }
        else {
            PageHelper.startPage(1, count <= 10 && count >= 1 ? count : 10);
            return comm.selectSortUserNews(user_id);
        }

    }
    //最近发表的文章
    public List<t_article> recentArticles(int count,int user_id){

        String aut = iuse.SelectAuthority(user_id);
        List<t_article> all = null;
        if(aut.contains("root")) {
            PageHelper.startPage(1, count <= 10 && count >= 1 ? count : 10);
            all = arti.selectGetAll();
        }
        else {
            PageHelper.startPage(1, count <= 10 && count >= 1 ? count : 10);
            all = arti.selectGetUserAll(user_id);
        }
        //加入点击量和评论
        for(int i=0;i<all.size();i++){
            t_article a = all.get(i);
            t_statistic statistic = stat.getStatisticById(a.getId());
            if(statistic==null){
                stat.addStatistic(a);
                statistic = stat.getStatisticById(a.getId());
            }
            a.setHits(statistic.getHits());
            a.setComments_num(statistic.getComments_num());

        }
        return all;
    }
    //获取后台统计数据
    public StaticticsBo getStatistic(int user_id){
        StaticticsBo bo = new StaticticsBo();
        String aut = iuse.SelectAuthority(user_id);

        if(aut.contains("root")) {
            bo.setComments(comm.countComment());
            bo.setArticles(arti.countArticle());
        }
        else {
            bo.setComments(comm.countCommentser(user_id));
            bo.setArticles(arti.countArticleUser(user_id));
        }

        return bo;
    }
    //更新某个文章统计数据（hits+1）
    public void updateStatistic(t_article article){
        t_statistic statistic = stat.getStatisticById(article.getId());
        statistic.setHits(statistic.getHits()+1);
       //更新缓存
        article.setHits(article.getHits()+1);
        try{redis.opsForValue().set("article_"+article.getId(),article);}
        catch (Exception e){
            logger.error("更新缓存失败，"+e.getMessage());
        }
        stat.updateStatisticByArticleId(statistic);
    }
}
